package csu.web.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.mypetstore.mapper.ItemMapper;
import csu.web.mypetstore.mapper.ProductMapper;
import csu.web.mypetstore.pojo.Item;
import csu.web.mypetstore.pojo.Product;
import csu.web.mypetstore.service.ItemService;
import csu.web.mypetstore.service.ProductService;
import csu.web.mypetstore.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 18632
 * @description 针对表【product】的数据库操作Service实现
 * @createDate 2025-03-10 16:27:13
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService{
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public Product getProduct(String productId) {

        Wrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .eq(Product::getProductid, productId);

        return productMapper.selectOne(wrapper);
    }
    @Override
    public Product getProductByItemId(String itemId) {

        Wrapper<Item> wrapper = new LambdaQueryWrapper<Item>()
                .eq(Item::getItemid, itemId);
        Item item = itemMapper.selectOne(wrapper);
        if (item!= null) {
            Wrapper<Product> productWrapper = new LambdaQueryWrapper<Product>()
                    .eq(Product::getProductid, item.getProductid());
            return productMapper.selectOne(productWrapper);
        }


        return null;
    }
    @Override
    public List<Product> getProductListByCategory(String categoryId) {
        return productMapper.selectList(
                new LambdaQueryWrapper<Product>()
                        .eq(Product::getCategory, categoryId) // 查询条件：categoryId
        );
    }
    @Override
    public List<Product> searchProductNameList(String keyword) {
        return productMapper.selectList(
                new LambdaQueryWrapper<Product>()
                        .like(Product::getName, keyword)   // 商品名称匹配
                         // 商品描述匹配
        );
    }
    @Override
    public List<Product> searchProductDescriptionList(String keyword) {
        return productMapper.selectList(
                new LambdaQueryWrapper<Product>()
                        .like(Product::getDescn, keyword)   // 商品名称匹配
                // 商品描述匹配
        );
    }
    @Override
    public void addNewProduct(Product product) {
        productMapper.insert(product);
    }
    @Override
    public void deleteProduct(String productId) {
        Wrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .eq(Product::getProductid, productId);
        productMapper.delete(wrapper);
    }
    @Override
    public void updateProductName(String name, String productId) {
        Product product = new Product();
        product.setName(name);
        Wrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .eq(Product::getProductid, productId);
        productMapper.update(product, wrapper);
    }
    @Override
    public void updateProductDescn(String descn, String productId) {
        Product product = new Product();
        product.setDescn(descn);
        Wrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .eq(Product::getProductid, productId);
        productMapper.update(product, wrapper);
    }
    @Override
    public void updateProductCategory(String category, String productId) {
        Product product = new Product();
        product.setCategory(category);
        Wrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                .eq(Product::getProductid, productId);
        productMapper.update(product, wrapper);
    }
    @Override
    public Result searchProduct(String keyword) {
        List<Product> productNameList = searchProductNameList(keyword);
        return Result.success(productNameList);

    }
}




